﻿using Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace WinFormsPrintDemo
{
    class TablePrint
    {
        private PrintDocument pd = new PrintDocument(); //打印对象
        private List<PsdEntity> PsdData = null; //打印数据
        private int TotalPage = 0;//总页数
        private int CurrentPage = 1;//当前页

        private Pen linePen = new Pen(Color.Black, 0.01F);//线
        private float w = 207F; //宽度
        private float l = 0.5F; //左边距
        private float t = 15.0F; //上边距
        private float rowH = 6.8F; //行高

        private Font sFont = new Font("宋体", 12, FontStyle.Regular);
        private Font sFontBold = new Font("宋体", 12, FontStyle.Bold);

        public TablePrint(List<PsdEntity> psdData)
        {
            this.PsdData = psdData;
            this.TotalPage = psdData.Count;

            //换算=毫米 * 100 / 25.4 + 1
            this.pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 856, 548);
            this.pd.PrintPage += new PrintPageEventHandler(this.CreateDocument);
            this.pd.BeginPrint += Pd_BeginPrint;
            this.pd.EndPrint += Pd_EndPrint;

            linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; //虚线
        }

        /// <summary>
        /// 打印
        /// </summary>
        public void Print()
        {
            pd.Print();//打印
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        public void PrintPriview()
        {
            try
            {
                PrintPreviewDialog PrintPriview = new PrintPreviewDialog
                {
                    Document = pd,
                    WindowState = FormWindowState.Maximized
                };
                PrintPriview.ShowDialog();//预览
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        //开始打印事件
        private void Pd_BeginPrint(object sender, PrintEventArgs e)
        {
            //MessageBox.Show("开始打印");
        }

        //结束打印事件
        private void Pd_EndPrint(object sender, PrintEventArgs e)
        {
            if (TotalPage > 1 && CurrentPage > 1)
            {
                CurrentPage = 1;
            }
        }

        //创建文档
        private void CreateDocument(object sender, PrintPageEventArgs e)
        {
            if (CurrentPage < TotalPage)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
            }
            PSD(e.Graphics, CurrentPage);
            CurrentPage++;
        }

        //渲染内容
        private void PSD(Graphics g, int pageIndex)
        {
            PsdEntity data = PsdData[pageIndex - 1];
            g.PageUnit = GraphicsUnit.Millimeter;//设置为毫米

            //===画框
            g.DrawRectangle(linePen, l, t, w, rowH * 14);//大边框

            //横线
            g.DrawLine(linePen, l, t + rowH * 1, l + w - rowH * 4, t + rowH);
            g.DrawLine(linePen, l, t + rowH * 2, l + w - rowH * 4, t + rowH * 2);
            g.DrawLine(linePen, l, t + rowH * 3, l + w - rowH * 4, t + rowH * 3);
            g.DrawLine(linePen, l, t + rowH * 4, l + w, t + rowH * 4);
            g.DrawLine(linePen, l, t + rowH * 5, l + w, t + rowH * 5);
            g.DrawLine(linePen, l, t + rowH * 6, l + w, t + rowH * 6);
            g.DrawLine(linePen, l + 20.0F, t + rowH * 7, l + w, t + rowH * 7);
            g.DrawLine(linePen, l + 20.0F, t + rowH * 8, l + w, t + rowH * 8);
            g.DrawLine(linePen, l + 20.0F, t + rowH * 9, l + w, t + rowH * 9);
            g.DrawLine(linePen, l, t + rowH * 10, l + w, t + rowH * 10);
            g.DrawLine(linePen, l, t + rowH * 11, l + w, t + rowH * 11);
            g.DrawLine(linePen, l, t + rowH * 12, l + w, t + rowH * 12);
            g.DrawLine(linePen, l + 120F, t + rowH * 13, l + w, t + rowH * 13);

            //竖线
            g.DrawLine(linePen, l + 20F, t, l + 20F, t + rowH * 14);
            g.DrawLine(linePen, l + 91.8F, t, l + 91.8F, t + rowH * 2);
            g.DrawLine(linePen, l + 111.8F, t, l + 111.8F, t + rowH * 2);
            g.DrawLine(linePen, l + w - rowH * 4, t, l + w - rowH * 4, t + rowH * 4);//二维码左
            g.DrawLine(linePen, l + 60.0F, t + rowH * 6, l + 60F, t + rowH * 11);//产品名称右
            g.DrawLine(linePen, l + 100.0F, t + rowH * 6, l + 100F, t + rowH * 11);//胚子右
            g.DrawLine(linePen, l + 120.0F, t + rowH * 6, l + 120F, t + rowH * 11);//规格右
            g.DrawLine(linePen, l + 120.0F, t + rowH * 12, l + 120F, t + rowH * 14);//规格右(延长)
            g.DrawLine(linePen, l + 140.0F, t + rowH * 6, l + 140F, t + rowH * 11);//数量右
            g.DrawLine(linePen, l + 140.0F, t + rowH * 12, l + 140F, t + rowH * 14);//数量右(延长)
            g.DrawLine(linePen, l + 160.0F, t + rowH * 6, l + 160F, t + rowH * 11);//单价右
            g.DrawLine(linePen, l + 160.0F, t + rowH * 12, l + 160F, t + rowH * 14);//单价右(延长)
            g.DrawLine(linePen, l + 180.0F, t + rowH * 6, l + 180F, t + rowH * 11);//折扣右
            g.DrawLine(linePen, l + 180.0F, t + rowH * 12, l + 180F, t + rowH * 14);//折扣右(延长)


            //测量字符串的高度和宽度
            SizeF szfTitle = g.MeasureString("订单号", sFontBold);
            //g.DrawString("订单号 宽度:" + szfTitle.Width + " 高度:" + szfTitle.Height, sFontBold, Brushes.Black, 1 , 1 );

            //===填充数据
            //基本信息
            //pen.DrawImage(QrCodeUtil.Code128("19061799999001", 250, 30),80,1);
            g.DrawString("订 单 号", sFontBold, Brushes.Black, l + 1, t + 1.5F);
            g.DrawString(data.oid, sFont, Brushes.Black, l + 21, t + 1.5F);
            g.DrawString("会 员 号", sFontBold, Brushes.Black, l + 1 + 91.8F, t + 1.5F);
            g.DrawString(data.cid, sFont, Brushes.Black, l + 112F, t + 1.5F);
            g.DrawString("收 货 人", sFontBold, Brushes.Black, l + 1, t + 1.5F + rowH);
            g.DrawString(data.consignee, sFont, Brushes.Black, l + 21, t + 1.5F + rowH);
            g.DrawString("联 系 人", sFontBold, Brushes.Black, l + 1 + 91.8F, t + 1.5F + rowH);
            g.DrawString("13366751503 16677751503", sFont, Brushes.Black, l + 112F, t + 1.5F + rowH);
            g.DrawString("收货地址", sFontBold, Brushes.Black, l + 1, t + 1.5F + rowH * 2);
            g.DrawString(data.address, sFont, Brushes.Black, l + 21, t + 1.5F + rowH * 2);
            g.DrawString("生产备注", sFontBold, Brushes.Black, l + 1, t + 1.5F + rowH * 3);
            g.DrawString("写生日快乐  ！！！！", sFont, Brushes.Black, l + 21, t + 1.5F + rowH * 3);
            g.DrawString("页码" + pageIndex, sFontBold, Brushes.Black, 190, t + 1.5F);
            g.DrawImage(QrCodeUtil.QrCode("19061799999001", 98, 0), l + w - rowH * 4 + 1, t + 1);


            //产品信息
            g.DrawString("产品信息", sFontBold, Brushes.Black, l + 1F, t + 4.5F + rowH * 7);

            //配送信息
            g.DrawString("配送时间", sFontBold, Brushes.Black, l + 1F, t + 1.5F + rowH * 10);

            //配送信息
            g.DrawString("附件信息", sFontBold, Brushes.Black, l + 1F, t + 1.5F + rowH * 11);


            //温馨提示
            g.DrawString("温馨提示", sFontBold, Brushes.Black, l + 1F, t + 4.5F + rowH * 12);
            g.DrawString("订单来源", sFontBold, Brushes.Black, l + 120.8F, t + 1.5F + rowH * 12);
            g.DrawString("已付金额", sFontBold, Brushes.Black, l + 120.8F, t + 1.5F + rowH * 13);
            g.DrawString("应收金额", sFontBold, Brushes.Black, l + 160.8F, t + 1.5F + rowH * 12);
            g.DrawString("客户签字", sFontBold, Brushes.Black, l + 160.8F, t + 1.5F + rowH * 13);
        }
    }
}
